using Microsoft.EntityFrameworkCore;
using NLog.Web;
using SmartTracking.Infrastructure.Implements;
using SmartTracking.Infrastructure.Interfaces;
using SmartTracking.Infrastructure;
using System.Reflection;
using Yitter.IdGenerator;
using SmartTracking.API.Read.Application.Commands.User_command;
using SmartTracking.API.Read.DTO;
using SmartTracking.Domain.UserInfo;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
using SmartTracking.API.Read.Application.Commands.ZXL.Deathr;
using B.S.BaseData.Domain;
using SmartTracking.API.Read.EweDTO;
using SmartTracking.Domain.EWE;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

// ���ݿ�
builder.Services.AddDbContext<DataDbContext>(x =>
{
    x.UseMySql(builder.Configuration.GetConnectionString("db"),
        ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("db")));

    //x.UseMySql(connection,MySqlServerVersion.AutoDetect(connection));
});
// �ִ�
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
// �м��
builder.Services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
// ӳ��AutoMapper
builder.Services.AddAutoMapper(x =>
{
    x.CreateMap<Users, UserDto>();
    x.CreateMap<Roles, RolePermissionDto>();
    // EWE 相关映射
    x.CreateMap<Pregnancys, MainPregnancyDTO>()
     .ForMember(dest => dest.MainPregnancyTime, opt => opt.Ignore())
     .ForMember(dest => dest.SerialNumber, opt => opt.Ignore())
     .ForMember(dest => dest.Remarks, opt => opt.Ignore());
    x.CreateMap<Ewedeliverys, MainEweDeliveryDTO>()
     .ForMember(dest => dest.MainDeliveryTime, opt => opt.Ignore())
     .ForMember(dest => dest.SerialNumber, opt => opt.Ignore())
     .ForMember(dest => dest.Remarks, opt => opt.Ignore());
    x.CreateMap<Weanings, MainWeaningDTO>()
     .ForMember(dest => dest.MainWeaningTime, opt => opt.Ignore())
     .ForMember(dest => dest.SerialNumber, opt => opt.Ignore())
     .ForMember(dest => dest.Remarks, opt => opt.Ignore());
    x.CreateMap<MainPregnancy, MainPregnancyDTO>()
     .ForMember(dest => dest.PregnancyTime, opt => opt.MapFrom(src => src.MainPregnancyTime))
     .ForMember(dest => dest.PregnancySnowId, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancyResult, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancyMethod, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancySameNum, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancyDayNum, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancyPoultryhome, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancyField, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancyTransferhouse, opt => opt.Ignore())
     .ForMember(dest => dest.PregnancyTransfer, opt => opt.Ignore())
     .ForMember(dest => dest.Reviewer, opt => opt.Ignore())
     .ForMember(dest => dest.ReviewerTime, opt => opt.Ignore())
     .ForMember(dest => dest.DocumentNumber, opt => opt.Ignore());
});
// ��־
builder.Host.UseNLog();
// ѩ��
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());

//��Ȩ��֤��ʹ��JWT��
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
{
    // ˽Կ
    var secretByte = Encoding.UTF8.GetBytes("kdsfldsflkdslkflkdsflkdslfdslkflk");
    o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
    {
        // ��֤������
        ValidateIssuer = true,
        // ��������Ϣ
        ValidIssuer = "BeiJingBW",

        // ��֤������
        ValidateAudience = true,
        // ������
        ValidAudience = "BeiJingBW",

        // ��֤�Ƿ����
        ValidateLifetime = true,

        // ��֤˽Կ
        IssuerSigningKey = new SymmetricSecurityKey(secretByte)

    };
});
// ����Swagger
builder.Services.AddSwaggerGen(o =>
{
    // ��ʾ ��Ȩ��Ϣ
    o.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme()
    {
        Description = "����JWT��ȨToken��Bearer Tokenֵ",
        Name = "Authorization",
        In = Microsoft.OpenApi.Models.ParameterLocation.Header,
        Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
        BearerFormat = "JWT",
        Scheme = "Bearer"
    });
    o.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            new string[]
            {

            }
        }
    });
});

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(x =>
{
    var path = AppDomain.CurrentDomain.BaseDirectory + "SmartTracking.API.Read.xml";
    x.IncludeXmlComments(path, true);
});

var app = builder.Build();

// ����
app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
    app.UseSwagger();
    app.UseSwaggerUI();
//}
// ��Ȩ
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
